﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class PositionExtraDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public PositionExtraDAL()
        {

        }
        public DataSet LoadPositionExtra(Nullable<int> Sequence, Nullable<int> PosSeq, Nullable<int> ClassSeq)
        {
            string strSQL;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@PosSeq", PosSeq);
                dbManager.AddParameters(2, "@ClassSeq", ClassSeq);
                dbManager.AddParameters(3, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

                strSQL = "SELECT ex.*"
                        + ", p.POS_REF, p.POS_NAME, c.CLASS_NAME, t.EXT_NAME"
                    + " FROM HRM_MP_POSITION_EXTRAS ex"
                        + " LEFT JOIN HRM_MP_POSITIONS p ON ex.POS_SEQ = p.POS_SEQ"
                        + " LEFT JOIN HRM_MP_CLASSES c ON ex.CLASS_SEQ = c.CLASS_SEQ" 
                        + " LEFT JOIN HRM_PR_EXTRA_TYPES t ON ex.EXT_SEQ = t.EXT_SEQ"
                    + " WHERE ex.RECORD_STATUS = @ActiveRecord"
                    + (Sequence.HasValue ? " AND ex.POS_EXT_SEQ = @Sequence" : "")
                    + (PosSeq.HasValue ? " AND p.POS_SEQ = @PosSeq" : "")
                    + (ClassSeq.HasValue ? " AND c.CLASS_SEQ = @ClassSeq" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return ds;
        }
        public void Insert(int PosSeq, int ClassSeq, int ExtSeq, Double Amount, string Remarks, int CreatedBy)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@PosSeq", PosSeq);
                dbManager.AddParameters(1, "@ClassSeq", ClassSeq);
                dbManager.AddParameters(2, "@ExtSeq", ExtSeq);
                dbManager.AddParameters(3, "@Amount", Amount);
                dbManager.AddParameters(4, "@Remarks", Remarks);
                dbManager.AddParameters(5, "@CreatedBy", CreatedBy);
                dbManager.AddParameters(6, "@CreatedWhen", DateTime.Today);
                dbManager.AddParameters(7, "@RecordStatus", SystemInfo._ActiveRecordStatus);

                string strSQL;

                strSQL = "INSERT INTO HRM_MP_POSITION_EXTRAS"
                    + "(POS_SEQ, CLASS_SEQ, EXT_SEQ, AMOUNT, REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@PosSeq, @ClassSeq, @ExtSeq, @Amount, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.Dispose();
            }
            catch
            {
                throw;
            }
        }
        public void Update(int Sequence, int ClassSeq, int ExtSeq, Double Amount, string Remarks, int UpdatedBy)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@ClassSeq", ClassSeq);
                dbManager.AddParameters(2, "@ExtSeq", ExtSeq);
                dbManager.AddParameters(3, "@Amount", Amount);
                dbManager.AddParameters(4, "@Remarks", Remarks);
                dbManager.AddParameters(5, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(6, "@UpdatedWhen", DateTime.Today);
                dbManager.AddParameters(7, "@RecordStatus", SystemInfo._ActiveRecordStatus);

                string strSQL;

                strSQL = "UPDATE HRM_MP_POSITION_EXTRAS"
                    + " SET CLASS_SEQ = @ClassSeq"
                        + ", EXT_SEQ = @ExtSeq"
                        + ", AMOUNT = @Amount"
                        + ", REMARKS = @Remarks"
                        + ", RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE POS_EXT_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.Dispose();
            }
            catch
            {
                throw;
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(2, "@UpdatedWhen", DateTime.Today);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._DeleteRecordStatus);

                string strSQL;

                strSQL = "UPDATE HRM_MP_POSITION_EXTRAS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE POS_EXT_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.Dispose();
            }
            catch
            {
                throw;
            }
        }
    }
}
